import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { fetchStudents } from "./studentFetch.js";

// Create an MCP server
const server = new McpServer({
    name: "FirstMcpServer",
    version: "1.0.0"
});

// hello world
server.tool("tell_weird_joke",
    { topic: z.string() },
    async ({ topic }) => ({
        content: [{ type: "text", text: `The weird joke about ${topic} is balabalabala!` }]
    })
);
// hello world
server.tool("student_query",
    { age: z.number(), email: z.string() },
    async ({ }) => {
        // const list = await fetchStudents();
        // return {
        //     content: [
        //         {
        //             type: "text",
        //             text: `查询结果为json格式，数据如下：${JSON.stringify(list)}`
        //         }
        //     ]
        // }

        // Mock 数据
        return {
            content: [
                {
                    type: "text",
                    text: `查询结果为json格式，数据如下：[{ "id": 11, "name": "hl", "age": 18, "email": "fedhong.com", "created_at": "2025-06-04T02:00:53.000Z", "updated_at": "2025-06-04T02:00:53.000Z" }, { "id": 10, "name": "王十二", "age": 33, "email": "wangshier@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 9, "name": "郑十一", "age": 26, "email": "zhengshiyi@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 8, "name": "吴十", "age": 29, "email": "wushi@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 7, "name": "周九", "age": 31, "email": "zhoujiu@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 6, "name": "孙八", "age": 27, "email": "sunba@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 5, "name": "钱七", "age": 35, "email": "qianqi@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 4, "name": "赵六", "age": 22, "email": "zhaoliu@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 3, "name": "王五", "age": 28, "email": "wangwu@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }, { "id": 2, "name": "李四", "age": 30, "email": "lisi@example.com", "created_at": "2025-06-03T14:58:52.000Z", "updated_at": "2025-06-03T14:58:52.000Z" }]`
                }
            ]
        }
    }
);

// Add an addition tool
server.tool("add",
    { a: z.number(), b: z.number() },
    async ({ a, b }) => ({
        content: [{ type: "text", text: String(a + b) }]
    })
);

// Add a dynamic greeting resource
server.resource(
    "greeting",
    new ResourceTemplate("greeting://{name}", { list: undefined }),
    async (uri, { name }) => ({
        contents: [{
            uri: uri.href,
            text: `Hello, ${name}!`
        }]
    })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);